package com.xinbochuang.machine.admin.controller.system;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xinbochuang.machine.admin.domain.system.SysStorehouse;
import com.xinbochuang.machine.admin.domain.system.SysStorehouseIn;
import com.xinbochuang.machine.admin.domain.system.SysStorehouseSecond;
import com.xinbochuang.machine.admin.service.system.ISysStorehouseInService;
import com.xinbochuang.machine.admin.service.system.ISysStorehouseSecondService;
import com.xinbochuang.machine.admin.service.system.ISysStorehouseService;
import com.xinbochuang.machine.common.controller.BaseController;
import com.xinbochuang.machine.common.domain.TableDataInfo;
import com.xinbochuang.machine.common.utils.SecurityUtils;
import com.xinbochuang.machine.common.utils.poi.ExcelUtil;
import com.xinbochuang.machine.framework.web.domain.AjaxResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 黄晓鹏
 * @date 2020-09-27
 */
@RestController
@RequestMapping("/system/storehouse-in")
public class SysStorehouseInController extends BaseController {

    @Resource
    private ISysStorehouseInService storehouseInService;

    @Resource
    private ISysStorehouseSecondService secondService;

    @Resource
    private ISysStorehouseService storehouseService;

    /**
     * 入库列表
     *
     * @param storehouseIn 入库
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('system:storehouse-in:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysStorehouseIn storehouseIn) {
        startPage();
        return getDataTable(storehouseInService.selectList(storehouseIn));
    }

    /**
     * 查询入库详细
     */
    @PreAuthorize("@ss.hasPermi('system:storehouse-in:query')")
    @GetMapping(value = "/{inId}")
    public AjaxResult getInfo(@PathVariable Integer inId) {
        return AjaxResult.data(storehouseInService.selectById(inId));
    }

    /**
     * 新增入库
     */
    @PreAuthorize("@ss.hasPermi('system:storehouse-in:add')")
    @PostMapping
    public AjaxResult add(@RequestBody List<SysStorehouseIn> storehouseInList) {
        return toAjax(storehouseInService.insertStorehouseIn(storehouseInList));
    }

    /**
     * 修改入库
     */
    @PreAuthorize("@ss.hasPermi('system:storehouse-in:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody SysStorehouseIn storehouseIn) {
        storehouseIn.setUpdateBy(SecurityUtils.getUsername());
        storehouseIn.setUpdateTime(DateUtil.now());
        return toAjax(storehouseInService.updateStorehouseIn(storehouseIn));
    }

    /**
     * 删除入库
     */
    @PreAuthorize("@ss.hasPermi('system:storehouse-in:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(storehouseInService.deleteByIds(ids));
    }


    /**
     * 修改入库状态
     */
    @PreAuthorize("@ss.hasAnyPermi('system:storehouse-in:check,system:storehouse-in:pass')")
    @PutMapping("/changeStatus")
    public AjaxResult check(@RequestBody SysStorehouseIn storehouseIn) {
        if(storehouseIn.getStatus().equals("6")){
            storehouseIn.setStatus("2");
        }
        return toAjax(storehouseInService.updateStatus(storehouseIn));
    }

    /**
     * 查询未删除的/在库的物品信息
     */
    @PreAuthorize("@ss.hasPermi('system:storehouse-in:list')")
    @GetMapping("/online")
    public AjaxResult online() {
        return AjaxResult.data(storehouseInService.online());
    }


    /**
     * 根据入库id查询二级清单
     */
    @PreAuthorize("@ss.hasPermi('system:storehouse-out:edit')")
    @GetMapping("/getSeconds")
    public AjaxResult getSeconds(Integer inId) {
        return AjaxResult.data(secondService.list(Wrappers.<SysStorehouseSecond>lambdaQuery().eq(SysStorehouseSecond::getFirstId, inId)));
    }

    /**
     * 根据入库id查询一级/二级清单
     */
    @PreAuthorize("@ss.hasPermi('system:storehouse-in:list')")
    @GetMapping("/getDetailList")
    public AjaxResult getDetailList(Integer inId) {
        List<SysStorehouse> storehouseList = storehouseService.list(Wrappers.<SysStorehouse>lambdaQuery().eq(SysStorehouse::getInId, inId));
        for (SysStorehouse storehouse : storehouseList) {
            storehouse.setChildren(secondService.list(Wrappers.<SysStorehouseSecond>lambdaQuery().eq(SysStorehouseSecond::getFirstId, storehouse.getId())));
        }
        return AjaxResult.data(storehouseList);
    }

    /**
     * 根据入库id查询一级/二级清单
     */
    @PreAuthorize("@ss.hasPermi('system:storehouse-in:list')")
    @GetMapping("/getDetail")
    public AjaxResult getDetail(String id) {
        List<SysStorehouse> storehouseList = storehouseService.list(Wrappers.<SysStorehouse>lambdaQuery().eq(SysStorehouse::getId, id));
        for (SysStorehouse storehouse : storehouseList) {
            storehouse.setChildren(secondService.list(Wrappers.<SysStorehouseSecond>lambdaQuery().eq(SysStorehouseSecond::getFirstId, storehouse.getId())));
        }
        return AjaxResult.data(storehouseList);
    }

    /**
     * 库存列表
     *
     * @param storehouseIn 入库
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('system:storehouse-in:list')")
    @GetMapping("/storelist")
    public TableDataInfo storelist(SysStorehouseIn storehouseIn) {
        startPage();
        return getDataTable(storehouseInService.storelist(storehouseIn));
    }

    /**
     * 导出待办
     */
    @PreAuthorize("@ss.hasPermi('system:storehouse-in:export')")
    @GetMapping("/export")
    public AjaxResult export(SysStorehouseIn storehouseIn) {
        List<SysStorehouseIn> list = storehouseInService.selectExportList(storehouseIn);
        ExcelUtil<SysStorehouseIn> util = new ExcelUtil<>(SysStorehouseIn.class);
        return util.exportExcel(list, "入库待办");
    }

    /**
     * 导出库存列表
     */
    @PreAuthorize("@ss.hasPermi('system:storehouse-in:exportStore')")
    @GetMapping("/exportStore")
    public AjaxResult exportStore(SysStorehouseIn storehouseIn) {
        List<SysStorehouse> list = storehouseInService.selectExportStoreList(storehouseIn);
        ExcelUtil<SysStorehouse> util = new ExcelUtil<>(SysStorehouse.class);
        return util.exportExcel(list, "库存列表");
    }
}

